package com.hbnu.client.handler;
import com.hbnu.message.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.channel.SimpleChannelInboundHandler;

import static com.hbnu.service.FileService.PROMISE_KEY;


public class ResponseHandler extends SimpleChannelInboundHandler<Response> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Response response) throws Exception {
        //响应失败
        ChannelPromise channelPromise = channelHandlerContext.channel().attr(PROMISE_KEY).get();
        if(response.isOk()){
            channelPromise.setSuccess();
        }else {
            channelPromise.setFailure(new RuntimeException("响应失败!"));
        }
        channelHandlerContext.close();
    }
}